package com.ming.dancing.springboot.controller;

import com.ming.dancing.springboot.entity.Dancer;
import com.ming.dancing.springboot.entity.User;
import com.ming.dancing.springboot.service.DancerService;
import com.ming.dancing.springboot.service.UserService;
import com.ming.dancing.springboot.utils.ValidateImageCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sun.jvm.hotspot.asm.Register;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.Map;


@Controller

public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private DancerService dancerService;
//    @DeleteMapping
//    @PutMapping
//    @GetMapping
    @PostMapping(value = "/user/login")
    public String login( String code,@RequestParam("username") String username, @RequestParam("password") String password, Map<String,Object> map,
                        HttpSession session, Model model){
        User user = userService.login(username, password);
        //System.out.println(user);
//        !StringUtils.isEmpty(username) && "123456".equals(password)
        if(user!=null)
        {
            //防止表单重复提交，可以重定向到主页。
            session.setAttribute("loginUser",username);
            System.out.println("登录的用户名是"+username);
            String sessioncode= (String) session.getAttribute("code");
            System.out.println(sessioncode+"-"+code);
            if(sessioncode.equalsIgnoreCase(code))
            {
                return "redirect:/main.html";
            }
            else
            {
                map.put("msg","验证码错误");
                return "login";
            }
        }
        else {
            map.put("msg","用户名密码错误");
            return "login";
        }

    }

    @PostMapping(value = "/user/register")
    public String register(User user,String code,HttpSession session,Model model,Map<String,Object> map)
    {
        String sessioncode= (String) session.getAttribute("code");
        if(sessioncode.equalsIgnoreCase(code))
        {
            userService.save(user);
            return "redirect:/tologin";
        }
        else
        {
            map.put("msg","验证码错误");
            return "register";
        }
    }

    //生成验证码
    @GetMapping("/code")
    public void getImage(HttpSession session, HttpServletResponse response) throws IOException {
        //生成验证码
        String securityCode = ValidateImageCodeUtils.getSecurityCode();
        BufferedImage image = ValidateImageCodeUtils.createImage(securityCode);
        session.setAttribute("code",securityCode);//存入session作用域中
        //响应图片
        ServletOutputStream os = response.getOutputStream();
        ImageIO.write(image,"png",os);
    }


    @GetMapping(value = "/toregister")
    public String toregister()
    {
        return "register";
    }
    @GetMapping(value = "/tologin")
    public String tologin()
    {
        return "login";
    }
    @GetMapping(value = "/toregister2")
    public String toregister2()
    {
        return "register-2";
    }
    @GetMapping(value = "/tologin2")
    public String tologin2()
    {
        return "login-2";
    }
}
